﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace EasyPiano
{
    public class MusicRecord
    {
        public string author;
        public string musicName;
        private List<MusicScore> simpleMusics = new List<MusicScore>();//简单模式
        private List<MusicScore2> continueMusics = new List<MusicScore2>();//持续模式
        public int startTime;
        public int mode;

        public int TotalProgress
        {
            get
            {
                if (mode == 0)
                    return simpleMusics.Count;
                else
                    return continueMusics.Count;
            }
        }
        public void Add(int index, int delay = 1)
        {
            simpleMusics.Add(new MusicScore(index, delay));
        }
        public void Add(int index, bool state,int time)
        {
            continueMusics.Add(new MusicScore2(index, state, time - startTime));
        }
        public void Save(string path)
        {
            //创建XML文档对象
            XmlDocument doc = new XmlDocument();
            //创建第一行描述信息，并且添加到doc文档中
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);
            //创建根节点
            XmlElement root = doc.CreateElement("MusicRecord");
            root.SetAttribute("Author", author);
            root.SetAttribute("MusicName", musicName);

            if (mode == 0)
            {
                root.SetAttribute("Mode", "Simple");
                for (int i = 0; i < TotalProgress; i++)
                {
                    //给根节点创建子节点
                    XmlElement node = doc.CreateElement("Score");
                    node.SetAttribute("String", simpleMusics[i].ScoreString);
                    node.SetAttribute("DelayTime", simpleMusics[i].duration.ToString());
                    //将子结点添加到根节点
                    root.AppendChild(node);
                }
            }
            else if (mode == 1)
            {
                root.SetAttribute("Mode", "Continue");
                for (int i = 0; i < TotalProgress; i++)
                {
                    //给根节点创建子节点
                    XmlElement node = doc.CreateElement("Score");
                    node.SetAttribute("String", continueMusics[i].ScoreString);
                    node.SetAttribute("State", continueMusics[i].state.ToString());
                    node.SetAttribute("Time", continueMusics[i].time.ToString());
                    //将子结点添加到根节点
                    root.AppendChild(node);
                }
            }

            //将根节点添加到文档中
            doc.AppendChild(root);
            //保存
            doc.Save(path);
        }
        public void Read(string path)
        {
            XmlDocument doc = new XmlDocument();
            //加载要读取的XML
            doc.Load(path);
            //获得根节点
            XmlElement root = doc.DocumentElement;
            musicName = root.Attributes["MusicName"]?.Value;
            author = root.Attributes["Author"]?.Value;
            string m = root.Attributes["Mode"]?.Value;
            if(m == "Continue")
                mode = 1;

            if (mode == 0)
            {
                //获得子节点 返回节点的集合
                XmlNodeList nodes = root.ChildNodes;
                for (int i = 0; i < nodes.Count; i++)
                {
                    string str = nodes[i].Attributes["String"].Value;
                    float delay = float.Parse(nodes[i].Attributes["DelayTime"].Value);
                    simpleMusics.Add(new MusicScore(str, delay));
                }
            }
            else if (mode == 1)
            {
                //获得子节点 返回节点的集合
                XmlNodeList nodes = root.ChildNodes;
                for (int i = 0; i < nodes.Count; i++)
                {
                    string str = nodes[i].Attributes["String"].Value;
                    bool state = bool.Parse(nodes[i].Attributes["State"].Value);
                    int time = int.Parse(nodes[i].Attributes["Time"].Value);
                    continueMusics.Add(new MusicScore2(str, state, time));
                }
            }
        }
        public void Clear()
        {
            simpleMusics.Clear();
            continueMusics.Clear();
        }
        public MusicScore GetSimple(int index)
        {
            if (index >= 0 && index < TotalProgress)
                return simpleMusics[index];
            return null;
        }
        public void SetSimpleDuration(int index,float duration)
        {
            if (index >= 0 && index < TotalProgress)
                simpleMusics[index].duration = duration;
        }
        public MusicScore2 GetContinue(int index)
        {
            if (index >= 0 && index < TotalProgress)
                return continueMusics[index];
            return null;
        }
        public int FindEqualBefore(int index, int soundIndex)
        {
            for (int i = index-1; i >= 0; i--)
            {
                if (continueMusics[i].soundIndex == soundIndex)
                {
                    return i;
                }
            }
            return -1;
        }
        public int FindEqualAfter(int index, int soundIndex)
        {
            for (int i = index + 1; i < TotalProgress; i++)
            {
                if (continueMusics[i].soundIndex == soundIndex)
                {
                    return i;
                }
            }
            return -1;
        }
    }
}
